


/* 

Name:  table_OA7.do 
Created: June , 2022
Aim: reproduce Nunn's paper with our measure of input specificty 

Inputs: 
-  speficity_hs6
-  judicial_quality 
-  export2004

Output: 
- $outputpath/table_online_7.xls

*/ 

cd $sectorpath 

set scheme s1color
use hs_sic_naics_exports_89_109_20111004, clear
keep if year==102 
g hs6=substr(commodity,1,6) 
duplicates drop hs6 , force 
keep naics hs6 
sort hs6 
save temp_naicshs6, replace

use $rspath/rs_hs02_19962006 , clear
g hs6=hs6_2002
sort hs6 	
merge 1:1 hs6 using temp_naicshs6
keep if _m==3 
drop _m 
sort hs6 
merge 1:m hs6_2002 using hs2002_to_sitc2.dta 
drop if _m==2 
drop _m

sort sitc2_4d 
merge m:1 sitc2_4d using rauch_sitc2_4digit.dta // Rauch measure
drop if _m==2 
rename _m _mm
rename lib lib_34 
rename con con_34 
sort sitc2_3d 
merge m:1 sitc2_3d using rauch_sitc2_3digit.dta // Rauch measure
replace lib_34 = lib if _mm==1 
replace con_34 = con if _mm==1 
drop if _merge==2
drop _m* 

duplicates drop hs6, force 
sort hs6 
merge 1:1 hs6 using isic.dta 
drop if _m==2 
drop _m 

tostring i2code, replace 
g industry_code=substr(i2code,1,3)
sort industry_code
merge m:1 industry_code using contract_intensity_ISIC_1997.dta // Nunn' measure of relationship specificity 
drop _m 

sort hs6 
merge 1:1 hs6 using upstreamness_hs6.dta // Antras et al. measure of upstreamness
drop if _m==2 
drop _m 

g hs4 = substr(hs6,1,4) 
g hs2 = substr(hs6,1,2) 

sort hs4 
merge m:1 hs4 using sigma_hs4_imbsmejean.dta // Imbs & Mejean measure of elasticity 
drop if _m==2 
drop _m 
save temp, replace 

use $countrypath/export2004, clear
tostring hs6, replace 
replace hs6="0"+hs6 if length(hs6)==5
keep iso3 hs6 v 
fillin iso3 hs6
replace v=0 if _f==1 
drop _f 
sort hs6 
g hs6_2002=hs6
merge m:1 hs6_2002 using temp
keep if _m==3
 drop _m 
sort iso3 
merge m:1 iso3 using judicial_quality 
keep if _m==3
drop _m 
 
egen c=group(iso3) 
egen prod=group(hs6) 

rename Area2i area2i
rename FLegalenforcementofcontractsi legalenf 
rename RLi rulelaw
rename rs_hs6_b specificity 

g rauch = lib_34=="Differentiated"
replace rauch=. if lib_34==""
foreach i in  specificity frac_lib_diff upstreamness rauch{
foreach j in area2i legalenf rulelaw{
g `i'_`j'=`i'*`j'
}
}

egen sect_nunn=group(industry_code)
egen tots=sum(v), by(hs6) 
egen tot=sum(v)
egen totc=sum(v), by(iso3) 

g balassa=(v/totc)/(tots/tot) 
g lx=log(v)
g ca=balassa>1

// Regressions
keep if frac_lib_diff_legalenf!=. & frac_lib_diff_rulelaw!=. 
drop c prod 
egen prod=group(hs6) 
egen c=group(iso3) 
egen cnunn=group(iso3 sect_nunn) 

reghdfe lx specificity_rulelaw , a(c prod) cluster(c )
outreg2 using $outputpath/table_online_7.xls, replace ctitle(log(exports)) dec(3) 

reghdfe lx frac_lib_diff_rulelaw, a(c prod) cluster(c )
outreg2 using $outputpath/table_online_7.xls, append ctitle(log(exports)) dec(3) 

reghdfe lx specificity_rulelaw frac_lib_diff_rulelaw upstreamness_rulelaw, a(c prod) cluster(c )
outreg2 using $outputpath/table_online_7.xls, append ctitle(log(exports)) dec(3) 

reghdfe balassa specificity_rulelaw frac_lib_diff_rulelaw upstreamness_rulelaw, a(c prod) cluster(c ) 
outreg2 using $outputpath/table_online_7.xls, append ctitle(Balassa Index) dec(3) 

reghdfe ca specificity_rulelaw frac_lib_diff_rulelaw upstreamness_rulelaw, a(c prod) cluster(c ) 
outreg2 using $outputpath/table_online_7.xls, append ctitle(Dummy CA) dec(3) 

erase temp_naicshs6.dta 
erase temp.dta 

